//
//  KPChangePasswordViewController.swift
//  KPBaseProject
//
//  Created by 孑孓 on 2018/4/4.
//  Copyright © 2018年 Kipp. All rights reserved.
//

import UIKit
import Eureka

class KPChangePasswordViewController: FormViewController  {

    override func viewDidLoad() {
        super.viewDidLoad()

        createUI()
    }

    // MARK: 创建视图
    func createUI() {
        self.title = "修改密码"
        view.backgroundColor = UIColor.white
        
        form +++ Section()
            <<< TextRow() { row in
                row.tag = "oldPasswordRow"
                row.title = "旧密码"
                row.placeholder = "请输入旧密码"
            }
            <<< TextRow() { row in
                row.tag = "newPasswordRow"
                row.title = "新密码"
                row.placeholder = "请输入新密码"
        }
        
        let changePasswordBtn = UIButton.init(type: .custom)
        changePasswordBtn.frame = CGRect.init(x: 20, y: Screen.height - 120, width: Screen.width - 40, height: 40)
        changePasswordBtn.setTitle("修改密码", for: .normal)
        changePasswordBtn.backgroundColor = UIColor.yellow
        changePasswordBtn.addTarget(self, action: #selector(changePasswordBtnClick(btn:)), for: UIControlEvents.touchUpInside)
        view.addSubview(changePasswordBtn)
    }

    // MARK: Action
    @objc func changePasswordBtnClick(btn :UIButton) {
        let dic = form.values(includeHidden: true)
        let oldPasswordRow = dic["oldPasswordRow"] as! String
        let newPasswordRow = dic["newPasswordRow"] as! String
        
        if oldPasswordRow.isEmpty {
            
        } else if newPasswordRow.isEmpty{
            
        }else{
            changePasswordRequest(oldPassword: oldPasswordRow, newPassword: newPasswordRow)
        }
    }
    
    // MARK: NetworkRequest
    fileprivate func changePasswordRequest(oldPassword: String, newPassword: String){
//        startAnimating(50.size, message: "正在加载...",messageFont: 16.font, type: NVActivityIndicatorType.init(rawValue: 30))
        Api.postJSONRequest(ApiType.修改密码, parameters: ["password":oldPassword, "newPwd":newPassword])
            .mapBaseModel()
            .subscribe(onNext: { (model) in
                KPLog("返回状态码：\(model.resultState)---返回请求结果：\(model.msg)---返回数据\(String(describing: model.data!))")
                if model.resultState == 1 {
                    KPLog("成功")
//                    self.stopAnimating()
                }else{
                    KPLog("失败")
//                    self.stopAnimating()
                }
                
            }, onError: { (error) in
//                self.stopAnimating()
            })
            .disposed(by: bag)
    }
}
